package com.powernode.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.Member;
import com.powernode.model.Result;
import com.powernode.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author RenBoQing
 * @date 2024年08月26日 20:40
 * @Description 后台管理系统维护会员控制层
 */
@Api(tags = "后台管理系统会员接口管理")
@RequestMapping("admin/user")
@RestController
public class SysMemberController {
    @Autowired
    private MemberService memberService;
    /*
     * 多条件分页查询会员
     * @author RenBoQing
     * @date 2024/8/26 22:28
     * @param current
     * @param size
     * @param nickName
     * @param status
     * @return com.powernode.model.Result<com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.powernode.domain.Member>>
     */
    @ApiOperation("多条件分页查询会员")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('admin:user:page')")
    public Result<Page<Member>> loadMemberPage(@RequestParam Long current, @RequestParam Long size, @RequestParam(required = false) String nickName, @RequestParam(required = false) Integer status) {
        // 创建会员分页对象
        Page<Member> page = new Page<>(current, size);
        // 多条件分页查询会员
        page = memberService.page(page, new LambdaQueryWrapper<Member>().eq(ObjectUtil.isNotNull(status), Member::getStatus, status).like(StringUtils.hasText(nickName), Member::getNickName, nickName).orderByDesc(Member::getCreateTime));
        // 返回对应的结果集 分页对象
        return Result.success(page);
    }
    /*
     * 根据id查询会员信息
     * @author RenBoQing
     * @date 2024/8/26 22:28
     * @param id
     * @return com.powernode.model.Result<com.powernode.domain.Member>
     */
    @ApiOperation("根据id查询会员信息")
    @GetMapping("info/{id}")
    @PreAuthorize("hasAuthority('admin:user:info')")
    public Result<Member> getMemberById(@RequestParam Long id) {
        // 根据id查询会员信息
        Member member = memberService.getOne(new LambdaQueryWrapper<Member>()
                // 这里仅查询部分字段
                .select(Member::getId, Member::getOpenId, Member::getNickName, Member::getPic).eq(Member::getId, id));
        // 返回对应的结果集
        return Result.success(member);
    }
     /*
      * 修改会员状态
      * @author RenBoQing
      * @date 2024/8/26 22:47
      * @param member
      * @return com.powernode.model.Result<java.lang.String>
      */
    @ApiOperation("修改会员状态")
    @PutMapping("")
    @PreAuthorize("hasAuthority('admin:user:update')")
    public Result<String> updateStatus(@RequestBody  Member member) {
        member.setUpdateTime(new Date());
        return Result.handle(memberService.updateById(member));
    }
    /*
     * 删除会员
     * @author RenBoQing
     * @date 2024/8/26 22:47
     * @param id
     * @return com.powernode.model.Result<java.lang.String>
     */
    @ApiOperation("删除会员")
    @DeleteMapping("")
    @PreAuthorize("hasAuthority('admin:user:delete')")
    public Result<String> deleteMember(@RequestParam Long id) {
        return Result.handle(memberService.removeById(id));
    }
    /*
     * 批量删除会员
     * @author RenBoQing
     * @date 2024/8/26 22:47
     * @param ids 会员Id集合
     * @return com.powernode.model.Result<java.lang.String>
     */
    @ApiOperation("批量删除会员")
    @DeleteMapping("")
    @PreAuthorize("hasAuthority('admin:user:delete')")
    public Result<String> deleteMembers(@RequestBody List<Long> ids) {
        return Result.handle(memberService.removeByIds(ids));
    }
    /*
     * 提供给order的远程接口
     * @author RenBoQing
     * @date 2024/8/31 22:08
     * @param openId
     * @return com.powernode.model.Result<java.lang.String>
     */
    @ApiOperation("提供给order的远程接口")
    @GetMapping("/getNickNameByOpenId")
    @PreAuthorize("hasAuthority('admin:user:info')")
    public Result<String> getNickNameByOpenId(@RequestParam String openId) {
        Member member = memberService.getOne(new LambdaQueryWrapper<Member>().select(Member::getNickName).eq(Member::getOpenId, openId));
        return Result.success(member.getNickName());
    }
}
